package com.leadbank.stormstudy.kafka;

import com.google.gson.Gson;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.BasicOutputCollector;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseBasicBolt;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Tuple;
import org.apache.storm.tuple.Values;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.Map;

public class FilterMessageBlot extends BaseBasicBolt {

    private final static Logger logger = LoggerFactory.getLogger(FilterMessageBlot.class);

    public void execute(Tuple input, BasicOutputCollector collector) {

        //DefaultRecordTrasnlator
        //public static final Fields FIELDS = new Fields(new String[]{"topic", "partition", "offset", "key", "value"});
        //读取订单数据
        String paymentInfoStr = input.getStringByField("value");
        //将订单数据解析成JavaBean
        PaymentInfo paymentInfo = new Gson().fromJson(paymentInfoStr, PaymentInfo.class);
        // 过滤订单时间,如果订单时间在2015.11.11这天才进入下游开始计算
        Date date = paymentInfo.getCreateOrderTime();
//        if (Calendar.getInstance().get(Calendar.DAY_OF_MONTH) != 31) {
//            return;
//        }
        System.out.println("paymentInfo is " + paymentInfoStr);
        logger.info("paymentInfo is " + paymentInfoStr);
        //collector.emit(new Values(paymentInfo.getOrderId(), paymentInfo.getCreateOrderTime().getTime(), paymentInfo.getNum()));
        collector.emit(new Values(paymentInfoStr));
    }

    public void declareOutputFields(OutputFieldsDeclarer declarer) {
        //declarer.declare(new Fields("orderId", "createOrderTime", "num"));
        declarer.declare(new Fields("jsonstr"));
    }

    @Override
    public void prepare(Map stormConf, TopologyContext context) {
        super.prepare(stormConf, context);
    }
}
